package com.cn.redis.properties;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;

@Component("com.cn.redis.properties.cp")
@ConfigurationProperties(
        prefix = "idgenerator.common",
        ignoreUnknownFields = false
)
public class CommonProperties {
    public static final String TYPE_DEFAULT = "default";
    public static final String TYPE_SF = "sf";
    public static final String REDIS_DEFAULT = "Lettuce";
    public static final String REDIS_JEDISCLUSTER = "JedisCluster";
    private String redis_type = "default";
    private String type = "default";
    private int step = 1000;
    private Map<String, Integer> stepCustomMap = new HashMap();
    private String preKey = "";

    public CommonProperties() {
    }

    public String getRedis_type() {
        return this.redis_type;
    }

    public void setRedis_type(String redis_type) {
        this.redis_type = redis_type;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getStep() {
        return this.step;
    }

    public void setStep(int step) {
        this.step = step;
    }

    public String getPreKey() {
        return this.preKey;
    }

    public void setPreKey(String preKey) {
        this.preKey = preKey;
    }

    public Map<String, Integer> getStepCustomMap() {
        return this.stepCustomMap;
    }

    public void setStepCustomMap(Map<String, Integer> stepCustomMap) {
        this.stepCustomMap = stepCustomMap;
    }

    public int getStepC(String key) {
        key = this.getKeyOriginal(key);
        return this.stepCustomMap.containsKey(key) ? (Integer)this.stepCustomMap.get(key) : this.step;
    }

    public String getKeyFull(String key) {
        if (!StringUtils.isEmpty(this.preKey)) {
            key = this.preKey + "." + key;
        }

        return key;
    }

    public String getKeyOriginal(String key) {
        if (!StringUtils.isEmpty(this.preKey) && key.startsWith(this.preKey + ".")) {
            key = key.substring(this.preKey.length() + 1);
        }

        return key;
    }
}
